<?php

namespace app\api\controller;

use app\common\service\WeChatMnpService;
use support\Db;
use support\Log;

class WechatMp extends  BaseApi
{
    /**
     * 不需要登录的方法
     * @var array
     */
    protected $noNeedLogin = ['wxService'];
    public function wxService()
    {

        $server = (new WeChatMnpService())->getServer();

        //微信图片安全检测
        $encryptMessage = $server->getDecryptedMessage()->toArray();
        $event = $encryptMessage["Event"];
        //图片安全检测
        if ($event == 'wxa_media_check') {

            $detailMsg = $encryptMessage["detail"];

            if ($detailMsg["suggest"] != 'pass') {
                $postId = Db::table("media_check")->where("trace_id", $encryptMessage["trace_id"])->value("post_id");
                Db::table("post")->where("id", $postId)->update(["img_list" => []]);
            }

            Db::table("media_check")->where("trace_id", $encryptMessage["trace_id"])->update(['is_risky' => $detailMsg["suggest"]]);
        }


        $response = $server->serve();

        return response($response->getBody()->getContents(), $response->getStatusCode(), $response->getHeaders());
    }
}